//
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std;

char mapa[2][51][51];
int t, i, j, k, n, m, a[2], b[2], c[2], d[2];

int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int melhor, tent;

void rec(int aa[2], int bb[2], int cc[2], int dd[2], int sum){
	if( (sum > melhor) || (sum > 50) )return;
	if(tent==625000000)return;
	
	int aaa, bbb, ccc, ddd;
	
	++tent;
	//printf("++%d:(%d, %d ) (%d, %d) == %d\n",sum,  aa[0], bb[0], cc[0], dd[0], tent);
	
	for(int ii=0;ii<4;++ii){
	
		aaa=aa[0]+dir[ii][0];
		bbb=bb[0]+dir[ii][1];
		ccc=cc[0]+dir[ii][0];
		ddd=dd[0]+dir[ii][1];

		if(aaa<0||aaa>=m)continue;
		if(bbb<0||bbb>=n)continue;
		if(ccc<0||ccc>=m)continue;
		if(ddd<0||ddd>=n)continue;
		
		//se foi pra configuracao anterior cont...
		if(aaa==aa[1] && bbb==bb[1] && ccc==cc[1] && ddd==dd[1] )continue;
		
		//se for parede volta!
		if(mapa[0][bbb][aaa]=='#'){
			bbb=bb[0];
			aaa=aa[0];
		}
		if(mapa[1][ccc][ddd]=='#'){
			ccc=cc[0];
			ddd=dd[0];
		}
		
		//se for buraco VAI POR OUTRO CAMINHO!
		if( (mapa[0][bbb][aaa]=='B')  ||  (mapa[1][ccc][ddd]=='B') ){
			continue;
		}
		
		//se permaneceu no mesmo lugar VAI POR OUTRO CAMINHO!
		if( (aaa==aa[0]) && (bbb==bb[0]) && (ccc==cc[0]) && (ddd==dd[0]) ){
			continue;
		}
		
		if( (mapa[0][bbb][aaa]=='F')  &&  (mapa[1][ccc][ddd]=='F') ){
			if(sum<melhor)melhor=sum;
			continue;
		}
		aa[1]=aa[0];
		bb[1]=bb[0];
		cc[1]=bb[0];
		dd[1]=cc[0];

		aa[0]=aaa;
		bb[0]=bbb;
		cc[0]=bbb;
		dd[0]=ccc;
		
		rec(aa,bb,cc,dd,sum+1);
		
	}
}

void process(){
	scanf("%d%d", &n, &m);
	for(i=0;i<2;++i)
		for(j=0;j<n;++j)
			scanf("%s", mapa[i][j] );
	
	
	
	for(k=0;k<2;++k)
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			if(mapa[k][i][j]=='R'){
				a[k] = j;
				b[k] = i;
			}

	melhor=60;
	//for(k=0;k<2;++k)printf("(%d %d) (%d %d)\n", a[k], b[k], c[k], d[k]);
	//printf("%2d:(%2d, %2d) (%2d, %d)\n", 0, a[0], b[0], a[1], b[1]);
	
	tent=0;
	int aa[2], bb[2], cc[2], dd[2];
	
	aa[1]=aa[0]=a[0];
	bb[1]=bb[0]=b[0];
	cc[1]=cc[0]=a[1];
	dd[1]=dd[0]=b[1];
	
	rec(aa, bb, cc, dd, 0);
	
	printf("%d\n", melhor);
}
int main(){
	freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);
	scanf("%d", &t);
	while(t--){
		process();
	}
	return 0;
}
